package com.spider.admin.service.account;

import com.spider.admin.store.primary.mapper.AccountServicePermMapper;
import com.spider.admin.store.primary.model.AccountPermissions;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AccountPermsServiceImpl implements AccountPermsService {

    @Resource
    private AccountServicePermMapper accountServicePermMapper;

    @Override
    public Map<String, String> getAccountsPermsForCache() {
        Map<String, String> map = new HashMap<>();
        List<AccountPermissions> list = accountServicePermMapper.getAllActivePermissions();
        String ts = null;
        String url = null;
        if (list != null && list.size() > 0) {
            for (AccountPermissions ap : list) {
                ts = map.get(ap.getTenantId());
                url = ap.getServiceUrl();
                url = url.endsWith("/") ? url : url + "/";
                if (StringUtils.isEmpty(ts)) {
                    map.put(ap.getTenantId(), "'" + url + "'");
                } else {
                    map.put(ap.getTenantId(), ts + ",'" + url + "'");
                }
            }
        }

        return map;
    }

}
